<template>
	<div>
		<el-dialog title="修改管理员" :visible.sync="editorVisible" width="30%" :before-close="closeEditorManager" @open="opendEnd">
			<el-form :model="manager" ref="addManager" label-width="100px">
			  <el-form-item label="用户名" prop="userCode">
			  	<el-input type="text" v-model="manager.userCode" placeholder="请输入用户名" :disabled="true"></el-input>
			  </el-form-item>
				<el-form-item label="持有人" prop="userName">
					<el-input type="text" v-model="manager.userName" placeholder="请输入持有人姓名"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="userPwd">
					<el-input type="password" v-model="manager.userPwd" placeholder="请输入密码"></el-input>
				</el-form-item>
			  <el-form-item label="确认密码" prop="toUserPwd">
			  	<el-input type="password" v-model="manager.toUserPwd" placeholder="请再次密码"></el-input>
			  </el-form-item>
			  <el-form-item label="账户状态" prop="status">
			    <el-switch
			      v-model="manager.onstatus" @change="onstatus(manager.onstatus)"
			      active-text="启用" inactive-text="禁用">
			    </el-switch>
			  </el-form-item>
				<el-form-item label="权限" prop="role" :rules="[
				     { required: true, message: '请选择权限', trigger: 'blur' },
				   ]">
					<el-select v-model="manager.role" placeholder="请选择权限">
						<el-option v-for="item in visiters" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="closeEditorManager">取 消</el-button>
				<el-button type="primary" @click="addManager('addManager')">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name: "",
		props: ["editorVisible","editorObj"],
		data() {
			return {
				manager: {
         userId:"",
         userCode:"",
         userName: "",
         //以下两个变量用于判断两次输入密码是否一致
         userPwd: "",
         toUserPwd:"",
         //role在数据库的默认值为1，所以直接显示内容即可，不需要通过内容判断转值
         role: "",
         //以下两个变量用于判断用户状态
         status:1,
         onstatus:true,
				},
        visiters: [{
        	value: 0,
        	label: '普通用户'
        }, {
        	value: 1,
        	label: '管理员'
        }, {
        	value: 2,
        	label: '超级管理员'
        }],
			}
		},
		methods:{
      //设置是否启用账户
      onstatus(status){
        console.log(status)
        if(status===1){
          this.manager.onstatus=true
        }else if(status===0){
          this.manager.onstatus=false
        }
        // console.log(this.manager.onstatus)
        // console.log(this.manager.status)
      },
      //清除文本框数据，返回相对应文本框的值
      clearData(){
          this.manager={
            userId:"",
            userCode:"",
            userName: "",
            //以下两个变量用于判断两次输入密码是否一致
            userPwd: "",
            toUserPwd:"",
            //role在数据库的默认值为1，所以直接显示内容即可，不需要通过内容判断转值
            role: "",
            //以下两个变量用于判断用户状态
            status:1,
            onstatus:true,
          }
          this.opendEnd()
      },
      //关闭
			closeEditorManager(){
				// console.log(this)
        this.clearData()
				this.$parent.editorVisible=false
			},
      //需要等待dialog 打开完毕后 在金数据渲染
      opendEnd(){
        this.manager.userId=this.$props.editorObj.userId
        this.manager.userCode=this.$props.editorObj.userCode
      	this.manager.userName=this.$props.editorObj.userName
      	this.manager.role=this.$props.editorObj.role
        this.manager.status=this.$props.editorObj.status
        this.onstatus(this.$props.editorObj.status)
      },
      //更新数据
			addManager(params) {
        var self = this
				this.$refs[params].validate((valid) => {
          if (valid) {
            var manager = JSON.parse(sessionStorage.getItem("manager"))
            //通过显示状态进行显示数据传值
            if(self.manager.onstatus===true){
              self.manager.status=1
            }else{
              self.manager.status=0
            }
            // 检测输入的数据中是否存在空格
            var usercode = this.manager.userCode
            var username = this.manager.userName
            var code1 = this.manager.userPwd;
            var code2 = this.manager.toUserPwd;
            //防止判断时数据null空值错误
            if(usercode==null){
              usercode=''
            }
            if(username==null){
              username=''
            }
            if(code1==null){
              code1=''
            }
            if(code2==null){
              code2=''
            }
            if(usercode.split(" ").length==1){
              if(username.split(" ").length==1){
                if(code1.split(" ").length==1||code2.split(" ").length==1){
                  /* 密码判断*/
                  if(code1===code2){
                    //判断用户名是否为空，如果为空则将账户作为用户名向后台传递
                    // if(self.manager.userName==null && self.manager.userName ===""){
                    //   self.manager.userName = self.manager.userCode
                    // }
                    // console.log(self.manager.userCode)
                    // console.log(self.manager.userName)
                    // console.log(self.manager.userPwd)
                    // console.log(self.manager.status)
                    // console.log(self.manager.role)
                    self.$http({
                        url:"/api/CakeShop/udpUser",
                        method:"post",
                        headers:{
                          token:manager.token
                        },
                        data:{
                          userId:self.manager.userId,
                          userCode: self.manager.userCode,
                          userName: self.manager.userName,
                          role: self.manager.role,
                          status:self.manager.status
                        }
                    }).then(function(res){
                      console.log(res)
                      if(res.data.code==201){
                        self.$message({
                          message: res.data.msg,
                          type: 'error',
                        });
                      }
                      if(res.data.udpSuccNum>0){
                        self.$message({
                          message: res.data.msg,
                          type: 'success',
                          onClose:function(){
                            self.clearData()
                            //关闭修改界面
                            self.closeEditorManager()
                            //根据修改数据返回当前页
                            self.$parent.getMemberList(sessionStorage.getItem("page"))
                          }
                        });
                      }else{
                        self.$message({
                          message: res.data.msg,
                          type: 'error',
                        });
                      }
                    })
                    // this.$message({
                    // 	message: '恭喜你，添加成功',
                    // 	type: 'success'
                    // });
                  }else{
                    self.$message({
                    	duration: 1500,
                    	message: "两次输入密码不一致!",
                    	type: "error",
                    	onClose: function() {
                    		self.loading = false;
                    	},
                    });
                  }
                }else{
                  self.$message({
                  	duration: 1500,
                  	message: "两次输入密码中存在非法字符空格!",
                  	type: "error",
                  	onClose: function() {
                  		self.loading = false;
                  	},
                  });
                }
              }else{
                self.$message({
                	duration: 1500,
                	message: "用户名中存在非法字符空格!",
                	type: "error",
                	onClose: function() {
                		self.loading = false;
                	},
                });
              }
            }else{
              self.$message({
              	duration: 1500,
              	message: "账号中存在非法字符空格!",
              	type: "error",
              	onClose: function() {
              		self.loading = false;
              	},
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
				});
			}
		}

	}
</script>

<style>
</style>
